package com.shxy.rlzy.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shxy.rlzy.entity.Dictionary;
import com.shxy.rlzy.mapper.DictionaryMapper;
import com.shxy.rlzy.service.IDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 字典管理实现类
 */
@Service
public class DictionaryService extends ServiceImpl<DictionaryMapper, Dictionary> implements IDictionaryService {

    private DictionaryMapper dictionaryMapper;

    @Autowired
    public void setDictionaryService(DictionaryMapper dictionaryMapper) {
        this.dictionaryMapper = dictionaryMapper;
    }

    /**
     * 将列表转换成json对象
     * @param dics
     * @return
     */
    private List<JSONObject> convert2Json(List<Dictionary> dics) {
        return dics.stream().map(dictionary -> {
            JSONObject obj = JSONUtil.createObj();
            obj.set("id", dictionary.getId());
            obj.set("name", dictionary.getName());
            obj.set("value", dictionary.getValue());
            return obj;
        }).toList();
    }

    /**
     * 根据数据类型查询列表
     * @param type
     * @return
     */
    private List<Dictionary> listByType(int type) {
        return dictionaryMapper.selectList(new QueryWrapper<Dictionary>().eq("dic_type", type));
    }

    /**
     * 获取指定类型的数据列表
     * @param type
     * @return
     */
    private List<JSONObject> getJsonsByType(int type) {
        return convert2Json(listByType(type));
    }

    @Override
    public List<JSONObject> getPolitics() {
        return getJsonsByType(1);
    }

    @Override
    public List<JSONObject> getEthics() {
        return getJsonsByType(2);
    }

    @Override
    public List<JSONObject> getProvinces() {
        return getJsonsByType(3);
    }

    @Override
    public List<JSONObject> getEduExps() {
        return getJsonsByType(5);
    }

    @Override
    public List<JSONObject> getEduDegrees() {
        return getJsonsByType(4);
    }

    @Override
    public List<JSONObject> getContactTypes() {
        return getJsonsByType(7);
    }

    @Override
    public List<JSONObject> getContactPeriods() {
        return getJsonsByType(6);
    }

    @Override
    public List<JSONObject> getRoleTypes() {
        return getJsonsByType(8);
    }

    @Override
    public List<JSONObject> getMaritalStatus() {
        return getJsonsByType(9);
    }

    @Override
    public List<JSONObject> getHealthyTypes() {
        return getJsonsByType(10);
    }
}
